/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaDecSteoVeedorException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaDecSteoVeedorRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.service.IFeriaDecSteoVeedorService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.FeriaDecSteoVeedor;

/**
 * @author Usuario
 * 
 */
public class FeriaDecSteoVeedorController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public FeriaDecSteoVeedorController() {
		this.feriaDecSteoVeedorListModel = new ListDataModel();
	}

	/** bean para el manejo del formulario de busqueda FeriaDecSteoVeedor */
	public FeriaDecSteoVeedor feriaDecSteoVeedorSearch = new FeriaDecSteoVeedor();

	/** Objeto contenedor del listado de FeriaDecSteoVeedor luego de una consulta. */
	private DataModel feriaDecSteoVeedorListModel;

	/** bean para el manejo del formulario de creacion/edicion. FeriaDecSteoVeedor */
	private FeriaDecSteoVeedor feriaDecSteoVeedor = new FeriaDecSteoVeedor();

	/** instancia de servicio feriaDecSteoVeedorService */
	private IFeriaDecSteoVeedorService feriaDecSteoVeedorService;

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchFeriaDecSteoVeedorByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getFeriaDecSteoVeedorService().load(1, 10,
					this.feriaDecSteoVeedorSearch);
			this.feriaDecSteoVeedorListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.feriaDecSteoVeedorListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.feriaDecSteoVeedorListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.FERIA_DEC_STEO_VEEDOR_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return String
	 */
	public String searchFeriaDecSteoVeedorByAgno() {
		String msg = "";
		try {
			List valores = null;
			valores = getFeriaDecSteoVeedorService().load(1, 10,
					this.feriaDecSteoVeedorSearch);
			this.feriaDecSteoVeedorListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.feriaDecSteoVeedorListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.feriaDecSteoVeedorListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.FERIA_DEC_STEO_VEEDOR_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return String
	 */
	public String searchFeriaDecSteoVeedorByAgno(IFeriaDecSteoVeedorService service) {
		String msg = "";
		try {
			List valores = null;
			valores = service.loadByAnio(1, 10,
					this.feriaDecSteoVeedorSearch);
			this.feriaDecSteoVeedorListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.feriaDecSteoVeedorListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.feriaDecSteoVeedorListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.FERIA_DEC_STEO_VEEDOR_SEARCH_VIEW;
	}
	
	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.feriaDecSteoVeedor.getId().longValue() == 0L) {
				feriaDecSteoVeedor.setOrden(new Long(feriaDecSteoVeedor.getOrdenStr()));
				getFeriaDecSteoVeedorService().insert(this.feriaDecSteoVeedor);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (FeriaDecSteoVeedorException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (FeriaDecSteoVeedorRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- feriaDecSteoVeedor " + msg, null, null);
		return AppConstants.FERIA_DEC_STEO_VEEDOR_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.feriaDecSteoVeedor.getId().longValue() != 0L) {
				feriaDecSteoVeedor.setOrden(new Long(feriaDecSteoVeedor.getOrdenStr()));
				getFeriaDecSteoVeedorService().update(this.feriaDecSteoVeedor);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (FeriaDecSteoVeedorException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (FeriaDecSteoVeedorRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.FERIA_DEC_STEO_VEEDOR_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createFeriaDecSteoVeedor() {
		feriaDecSteoVeedor = new FeriaDecSteoVeedor();
		feriaDecSteoVeedor.setId(new Long(0L));
		feriaDecSteoVeedor.setIdSorteo(feriaDecSteoVeedorSearch.getIdSorteo());
		return AppConstants.FERIA_DEC_STEO_VEEDOR_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateFeriaDecSteoVeedor() {
		Map params = getExternalContext().getRequestParameterMap();
		String feriaDecSteoVeedorDataIndex = (String) params
				.get("feriaDecSteoVeedorDataIndex");
		this.feriaDecSteoVeedor = (FeriaDecSteoVeedor) ((List) this.feriaDecSteoVeedorListModel
				.getWrappedData())
				.get(Integer.parseInt(feriaDecSteoVeedorDataIndex));
		return AppConstants.FERIA_DEC_STEO_VEEDOR_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelFeriaDecSteoVeedor() {
		return AppConstants.FERIA_DEC_STEO_VEEDOR_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String 	cancelFeriaDecSteoVeedorOpcion() {
		return AppConstants.FERIA_DEC_SORTEO_SEARCH_VIEW;
	}


	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public FeriaDecSteoVeedor getFeriaDecSteoVeedorSearch() {
		return feriaDecSteoVeedorSearch;
	}

	/**
	 * 
	 * @param feriaDecSteoVeedorSearch
	 */
	public void setferiaDecSteoVeedorSearch(FeriaDecSteoVeedor feriaDecSteoVeedorSearch) {
		this.feriaDecSteoVeedorSearch = feriaDecSteoVeedorSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getferiaDecSteoVeedorListModel() {
		return feriaDecSteoVeedorListModel;
	}

	/**
	 * 
	 * @param feriaDecSteoVeedorListModel
	 */
	public void setferiaDecSteoVeedorListModel(DataModel feriaDecSteoVeedorListModel) {
		this.feriaDecSteoVeedorListModel = feriaDecSteoVeedorListModel;
	}

	/**
	 * 
	 * @return
	 */
	public FeriaDecSteoVeedor getFeriaDecSteoVeedor() {
		return feriaDecSteoVeedor;
	}

	/**
	 * 
	 * @param feriaDecSteoVeedor
	 */
	public void setferiaDecSteoVeedor(FeriaDecSteoVeedor feriaDecSteoVeedor) {
		this.feriaDecSteoVeedor = feriaDecSteoVeedor;
	}

	/**
	 * obtencion del servicio feriaDecSteoVeedorService
	 * 
	 * @return
	 */
	public IFeriaDecSteoVeedorService getFeriaDecSteoVeedorService() {
		return feriaDecSteoVeedorService;
	}

	/**
	 * establecimiento del servicio feriaDecSteoVeedorService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param feriaDecSteoVeedorService
	 */
	public void setFeriaDecSteoVeedorService(
			IFeriaDecSteoVeedorService feriaDecSteoVeedorService) {
		this.feriaDecSteoVeedorService = feriaDecSteoVeedorService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
